package com.example.myjavafx.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

import java.io.File;
import java.net.MalformedURLException;

/**
 * 视频播放控制器
 * @author admin
 */
public class VideoController {
    @FXML
    private MediaView mediaView;

    @FXML
    private TextField urlField;

    private MediaPlayer mediaPlayer;

    @FXML
    private void initialize() {
        // 初始化时可以设置默认视频
         loadDefaultVideo();
    }

    @FXML
    private void loadVideo() {
        String url = urlField.getText().trim();
        if (url.isEmpty()) {
            showAlert("请输入有效的视频URL");
            return;
        }

        try {
            // 尝试创建Media对象
            Media media;
            if (url.startsWith("http://") || url.startsWith("https://")) {
                // 网络URL
                media = new Media(url);
            } else {
                // 本地文件路径
                File file = new File(url);
                if (!file.exists()) {
                    showAlert("文件不存在: " + url);
                    return;
                }
                media = new Media(file.toURI().toURL().toString());
            }

            // 如果之前有播放器正在运行，先停止它
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.dispose();
            }

            // 创建新的媒体播放器
            mediaPlayer = new MediaPlayer(media);
            mediaView.setMediaPlayer(mediaPlayer);

            // 设置媒体播放器事件处理
            mediaPlayer.setOnReady(() -> {
                // 媒体准备好后自动调整视图大小
                double width = mediaPlayer.getMedia().getWidth();
                double height = mediaPlayer.getMedia().getHeight();

                // 保持宽高比例
                if (width > 0 && height > 0) {
                    mediaView.setFitWidth(width);
                    mediaView.setFitHeight(height);
                }
            });

            mediaPlayer.setOnError(() -> {
                showAlert("视频加载错误: " + mediaPlayer.getError().getMessage());
            });

            // 自动播放
            mediaPlayer.play();

        } catch (MalformedURLException e) {
            showAlert("URL格式错误: " + e.getMessage());
        } catch (Exception e) {
            showAlert("加载视频时出错: " + e.getMessage());
        }
    }

    @FXML
    private void play() {
        if (mediaPlayer != null) {
            mediaPlayer.play();
        } else {
            showAlert("请先加载视频");
        }
    }

    @FXML
    private void pause() {
        if (mediaPlayer != null) {
            mediaPlayer.pause();
        } else {
            showAlert("请先加载视频");
        }
    }

    @FXML
    private void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        } else {
            showAlert("请先加载视频");
        }
    }

    private void showAlert(String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("错误");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

    // 可选：加载默认视频
    private void loadDefaultVideo() {
        try {
            // 这里可以设置一个默认的视频路径
            String defaultVideoPath = getClass().getResource("/videos/视频1.mp4").toExternalForm();
            Media media = new Media(defaultVideoPath);
            mediaPlayer = new MediaPlayer(media);
            mediaView.setMediaPlayer(mediaPlayer);
        } catch (Exception e) {
            // 默认视频加载失败，忽略错误
            System.err.println("无法加载默认视频: " + e.getMessage());
        }
    }
}
